<?php

/* 
 * Manager/SyntheseManager.php
 */

namespace RHX\Model\Manager;

use Zend\Db\Adapter\Adapter;
use Zend\View\Model\ViewModel;
use RHX\Model\Object\InstructionPaie;
use RHX\Model\Object\Synthese;

class SyntheseManager{
    /**
     *
     * @var Adapter
     */
    protected $adapter = null;
    
    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter){
	$this->adapter = $adapter;
    }
    
    /**
     * Adapteur de base de données
     * 
     * @return Adapter
     */
    public function getAdapter(){
	return $this->adapter;
    }
    
    /**
     * Chargement des Synthèses de paie
     * 
     * @param \Zend\Stdlib\Parameters $post
     * @return array
     */
    public function loadIndex($post){
	$instructionManager= new InstructionManager($this->getAdapter());
	$instructions = $instructionManager->loadPlanningContent($post);
	$save = ($post->action == InstructionPaie::CREATE) ? true : false;
	$syntheses = $this->performSynthese($instructions, $save);
        
	return $syntheses;
    }
   
    /**
     * Transformer un jeu d'instruction de paie en synthese de paie
     * 
     * @param array $instructions
     * @param boolean $save
     * @return \RHX\Model\Object\Synthese
     */
    public function performSynthese($instructions, $save = false){
	$tableSynthese = new \RHX\Model\Table\SyntheseTable($this->getAdapter());
	$excel = new ExcelManager($this->getAdapter());
	$syntheses = array();
	foreach($instructions as $inst){
	    $employe= $inst->getEmploye();
	    $taux = $employe->getTaux();
	    $synthese = new Synthese($this->getAdapter(), $inst->getArrayCopy());
	    $synthese->cp_acquis_n1 = 0.0;
	    $synthese->cp_du_n1 = 0.0;
	    $synthese->prov_cp = $excel->provCP($inst->mensualite, $taux, $synthese->cp_acquis_n1, $synthese->cp_du_n1);
	    $synthese->salaire_net = 0.0;
	    $synthese->vir1 = 0.0;
	    $synthese->vir2 = 0.0;
	    $synthese->chq = 0.0;
            
            if($save || $synthese->id <= 0){ // Si le groupe d'instrcution vient d'être crée OU, sinon, l'objet Instruction n'est pas encore sauvé
                //$idSynth = $tableSynthese->save($synthese);    
                //$synthese->id = $idSynth;
            }
	    $syntheses[] = $synthese;
	}
	return $syntheses;
    }
    
    /**
     * Site
     * 
     * @param type $siteId
     * @return \RHX\Model\Object\Site
     */
    public function getSite($siteId){
	$table = new \RHX\Model\Table\SiteTable($this->getAdapter());
	return $table->get($siteId);
    }
    
    /**
     * Période
     * 
     * @param type $periodeId
     * @return \RHX\Model\Object\Periode
     */
    public function getPeriode($periodeId){
	$table = new \RHX\Model\Table\PeriodeTable($this->getAdapter());
	return $table->get($periodeId);
    }
    
    /**
     * Liste des sites à afficher
     * 
     * @param array $append
     * @param array $exception
     * @return array
     */
    public function getSiteList($append = array(), $exception = array()){
	$siteTable = new \RHX\Model\Table\SiteTable($this->getAdapter());
	$siteList = $siteTable->getSelectList($append, $exception);
	return $siteList;
    }
    
    /**
     * Récupérer tout les Synthèse de paie de tout les employés
     * 
     * @param \Zend\Stdlib\Parameters $post
     * @return \Zend\View\Model\ViewModel
     */
    public function getTotal($post){
	$data = $this->loadIndex($post);
	$total = new ViewModel(array(
	    'syntheses' => $data,
	    'periode' => $post->periode,
	    'site' => $post->site,
		));
	return $total;
    }
    
}

